<template>
	<!-- 整改录入 -->
	<view class="p-10">
		<view class="card">
			<my-forms ref="valiForm"
				:model="currentkey == 1 ? valiFormData : currentkey == 2 ? valiFormData1 : valiFormData2"
				:label-position="'top'" :formItem="currentkey == 1 ? formData : currentkey == 2 ? formData1 : formData2"
				label-width="200rpx"></my-forms>
			<u-button type="primary" shape="circle" text="保存提交" @click="submit"></u-button>
		</view>
	</view>
</template>
<script setup>
	import {
		reactive,
		ref,
		unref
	} from 'vue'
	import {
		onLoad,
	} from '@dcloudio/uni-app'
	import {
		infoUserProfile
	} from '@/api/public.js'
	import {
		getHiddenRequireDetail,
		changeRectify,
		addPostpone,
		postBack
	} from '@/api/hidden-danger.js'
	import date from '@/utils/date.js'

	// 校验表单数据
	const formData = reactive([{
		label: '整改日期',
		disabled: true,
		prop: 'changeEndDate'
	}, {
		label: '整改单位',
		disabled: true,
		prop: 'trueChangeDeptName'
	}, {
		label: '整改人',
		disabled: true,
		prop: 'trueChangeUserName'
	}, {
		label: '整改措施',
		type: 'textarea',
		required: true,
		prop: 'changeInfo'
	}, {
		label: '图片上传',
		itemType: 'image',
		prop: 'filePaths'
	}])
	const formData1 = reactive([{
		label: '申请人',
		disabled: true,
		prop: 'userName'
	}, {
		itemType: 'date',
		type: 'date',
		label: '申请限制日期',
		required: true,
		prop: 'applyChangeDate'
	}, {
		label: '申请原因',
		type: 'textarea',
		required: true,
		prop: 'applyMessage'
	}, {
		itemType: 'select',
		type: 'GETuseList',
		label: '受理人',
		required: true,
		prop: 'examinUserId'
	}, ])
	const formData2 = reactive([{
		label: '退回理由',
		type: 'textarea',
		required: true,
		prop: 'backMessage'
	}, ])

	const valiForm = ref(null)
	let valiFormData = ref({}),
		valiFormData1 = ref({}),
		valiFormData2 = ref({}),
		apiFn = ref(null),
		currentkey = ref(1) // 1代表整改 2代表申请延期 3代表退回
	// 保存提交
	const submit = () => {
		valiForm.value.$refs.refMyForm.validate().then(async (res) => {
			valiFormData.value.changeEndDate = date(new Date())
			const {
				code
			} = await unref(apiFn)(unref(unref(currentkey) == 1 ? valiFormData : unref(currentkey) == 2 ?
				valiFormData1 : valiFormData2))
			if (code == 200) {
				uni.showToast({
					title: `${unref(currentkey) == 1 ? '整改' :unref(currentkey) == 2 ? '申请':'退回'}成功`
				})
				uni.navigateBack({
					delta: 2
				})
			}
		}).catch(err => {})
	}
	onLoad(async (e) => {
		const {
			key,
			id
		} = e
		currentkey.value = key
		if (key == 1) {
			const {
				data
			} = await getHiddenRequireDetail({
				id
			})
			valiFormData.value = data
			valiFormData.value.changeEndDate = date(new Date(), 'YYYY-MM-DD')
			apiFn.value = changeRectify
		} else if (key == 2) {
			const {
				data
			} = await infoUserProfile()
			valiFormData1.value.userName = data.user.userName
			valiFormData1.value.checkPlanId = id
			apiFn.value = addPostpone
		} else {
			valiFormData2.value.id = id
			apiFn.value = postBack
		}
	})
</script>
<style lang="scss" scoped>
	.card {
		padding: 20rpx;
		background-color: #fff;
		border-radius: 20rpx;
	}
</style>